package com.tanhua.sso.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 字段自动填充
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取变量
        Object created = getFieldValByName("created", metaObject);
        // 判断
        if (created == null) {
            // 赋值
            setFieldValByName("created", new Date(), metaObject);
        }

        Object update = getFieldValByName("update", metaObject);
        if (update == null) {
            setFieldValByName("update", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 由于每次新增或者更新数据都需要自动填充值,所以不进行判断直接赋值
        setFieldValByName("update", new Date(), metaObject);
    }
}
